package cn.terys.terystransactionclient;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.emagsoftware.ui.dialog.DialogManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.MathUtilities;
import cn.terys.terystransactionclient.data.NetManager;

public class RefundConfirmActivity extends BaseActivity
{

    public static final String INTENT_EXTRANAME_REFUNDRESULT = "REFUNDRESULT";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.refundconfirm);
        setTitle(R.string.refundconfirm_title);

        Intent intent = getIntent();
        TextView orderNum = (TextView) findViewById(R.id.order_num);
        final String orderStr = intent.getStringExtra(RefundActivity.INTENT_EXTRANAME_ORDERNUMBER);
        orderNum.setText(orderStr);
        TextView sum = (TextView) findViewById(R.id.sum);
        final String sumCentStr = intent.getStringExtra(RefundActivity.INTENT_EXTRANAME_SUM);
        String sumStr = MathUtilities.round2(MathUtilities.div(Long.parseLong(sumCentStr), 100, 2), 2);
        sum.setText(Html.fromHtml("<font color='red'><b>" + sumStr + "</b></font>&nbsp;&nbsp;" + getString(R.string.generic_sum_rmb_unit)));
        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                new GenericAsyncTask<String, Object, String[]>(RefundConfirmActivity.this)
                {
                    @Override
                    protected String[] doInBackgroundImpl(String... arg0) throws Exception
                    {
                        // TODO Auto-generated method stub
                        return NetManager.refund(MainActivity.BATCH_NUM, arg0[0], arg0[1]);
                    }

                    protected void onPostExecute(Object[] objs, final String[] result)
                    {
                        // TODO Auto-generated method stub
                        super.onPostExecute(objs, result);
                        final Context context = (Context) objs[0];
                        DialogManager.showAlertDialog(context, R.string.generic_dialog_title, R.string.refundconfirm_comfirm_success, new int[] { R.string.generic_dialog_btn_ok }, null, true, false)
                                .setOnDismissListener(new DialogInterface.OnDismissListener()
                                {
                                    @Override
                                    public void onDismiss(DialogInterface dialog)
                                    {
                                        // TODO Auto-generated method stub
                                        Intent intent = new Intent(context, RefundResultActivity.class);
                                        intent.putExtra(INTENT_EXTRANAME_REFUNDRESULT, result);
                                        context.startActivity(intent);
                                    }
                                });
                    }
                }.execute(orderStr, sumCentStr);
            }
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

    }

}